package com.wutong.erb.exception.base;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 认证异常
 * @author wutong
 */
@Getter
public class AuthorizationException extends BaseException {

    private final int code;
    private static final Map<Integer, AuthorizationException> AUTHORIZATION_EXCEPTION_MAP = new HashMap<>();

    public AuthorizationException(int code, String message) {
        super(2L, AUTHORIZATION_EXCEPTION_MAP, message);
        this.code = code;
        if (AUTHORIZATION_EXCEPTION_MAP.get(code) == null) {
            AUTHORIZATION_EXCEPTION_MAP.put(code, this);
        } else {
            throw new IllegalArgumentException("存在相同的异常码: " + code + " - " + AUTHORIZATION_EXCEPTION_MAP.get(code).getClass().getSimpleName());
        }
    }

    public static AuthorizationException getException(int code) {
        return AUTHORIZATION_EXCEPTION_MAP.get(code);
    }
}
